package com.my.study.rocketmq.transaction;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * 消费者（事务消息）
 *
 * @author yyb8152
 * @since 20220509
 **/
public class Consumer {
    public static void main(String[] args) throws Exception {
        //1.创建消费者Consumer，制定消费者组名(Push=>推的方式)
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("TransactionGroup12");
        //2.指定Nameserver地址
        consumer.setNamesrvAddr("39.101.165.196:9876");
        //3.订阅主题Topic和Tag
        consumer.subscribe("TransactionTopic", "*");

        //4.设置回调函数，处理消息(监听消息)
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            //接受信息内容
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgList, ConsumeConcurrentlyContext context) {
                for (MessageExt message : msgList) {
                    System.out.println("consumeThread=" + Thread.currentThread().getName() + "," + new String(message.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //5.启动消费者consumer
        consumer.start();
        System.out.println("启动消费者！===");
    }

}
